package com.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.beans.Person;
import com.bussiness.PersonBussinessProxy;
import com.constants.Constants;
import com.constants.ServiceEndPoints;
import com.constants.UIMessages;
import com.exceptions.DeleteCustomerWithOutstandingMovieException;
import com.exceptions.DeleteUserFailedException;
import com.exceptions.InvalidEmailException;
import com.exceptions.ServiceUnavailableException;
import com.exceptions.UserException;

/**
 * Servlet implementation class DeleteAccountServlet
 */
public class DeleteAccountServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	PersonBussinessProxy personBI = new PersonBussinessProxy();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteAccountServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			RequestDispatcher dispatcher;
			HttpSession session = request.getSession(true);
			session.setMaxInactiveInterval(Constants.sessionTimeOut);
			Person userData = (Person)session.getAttribute("PersonData");
			
			if(userData != null)
			{
				//Deleting the user with the respective email Id
				personBI.setEndpoint(ServiceEndPoints.PersonEndPoint);
				try
				{
					int flag = personBI.removeUser(userData.getEmailId());
					if(flag == 1) //Successful
					{
						//Remove all session data
						session= request.getSession();
						session.invalidate();
						request.setAttribute("emailException", UIMessages.MEMBERSHIP_DELETED_SUCCESS);
						dispatcher = getServletContext().getRequestDispatcher("/welcome.jsp");
						dispatcher.forward(request, response);
					}
					else  //Unsuccessful
					{
						request.setAttribute("ServerException", UIMessages.MEMBERSHIP_DELETED_FAILURE);
						session.setAttribute("ServerException", UIMessages.MEMBERSHIP_DELETED_FAILURE);
						dispatcher = getServletContext().getRequestDispatcher("/deletePage.jsp");
						dispatcher.forward(request, response);
					}
				}
				catch(UserException user)
				{
					session = request.getSession(true);
					request.setAttribute("ServerException", user.toString());
					session.setAttribute("ServerException", user.toString());
					dispatcher = getServletContext().getRequestDispatcher("/deletePage.jsp");
					dispatcher.forward(request, response);
				}
				catch(DeleteCustomerWithOutstandingMovieException delcust)
				{
					session = request.getSession(true);
					request.setAttribute("ServerException", delcust.toString());
					session.setAttribute("ServerException", delcust.toString());
					dispatcher = getServletContext().getRequestDispatcher("/deletePage.jsp");
					dispatcher.forward(request, response);
				}
				catch(ServiceUnavailableException servunavl)
				{
					session = request.getSession(true);
					request.setAttribute("ServerException", servunavl.toString());
					session.setAttribute("ServerException", servunavl.toString());
					dispatcher = getServletContext().getRequestDispatcher("/deletePage.jsp");
					dispatcher.forward(request, response);
				}
				catch(InvalidEmailException inv)
				{
					session = request.getSession(true);
					request.setAttribute("ServerException",inv.toString());
					session.setAttribute("ServerException", inv.toString());
					dispatcher = getServletContext().getRequestDispatcher("/deletePage.jsp");
					dispatcher.forward(request, response);

				}			
			}
			else
			{
				request.setAttribute("ServerException", UIMessages.MEMBERSHIP_DELETED_FAILURE);
				session.setAttribute("ServerException", UIMessages.MEMBERSHIP_DELETED_FAILURE);
				dispatcher = getServletContext().getRequestDispatcher("/deletePage.jsp");
				dispatcher.forward(request, response);
			}
			
		}
		catch(Exception e)
		{
			HttpSession session = request.getSession(true);
			request.setAttribute("ServerException", UIMessages.MEMBERSHIP_DELETED_FAILURE);
			session.setAttribute("ServerException", UIMessages.MEMBERSHIP_DELETED_FAILURE);
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/deletePage.jsp");
			dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
